package com.saodisheng.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    /**
     * 该方法去掉多个关键词的空格
     * 如： java jsp会被单独存在数组中
     * @param sString
     * @return
     */
	public static  String[] splitStringBySpace(String sString)
	{
        String[] subString=sString.split("\\s+");
 
        return subString;
        
	}

    /**
     * 拼接字符串，将java jsp php形式的字符串拼接为java|jsp|php形式
     * @param sString
     * @return
     */
	public static  String conactString(String sString) {
		String outString = "";
        String[] subString=sString.split("\\s+");
        
        for (int i = 0; i<subString.length; i++) {
        	//注意！！！最后不能拼接|符号
        	if (i != (subString.length-1)) {
        		outString +=subString[i]+"|";
			} else {
        		outString +=subString[i];
        	}
		}
        System.out.println(outString);
        return outString;
        
	}
	
	public static String conactString(String s, String op) {
		s = s+op;
		return s;
	}

    /**
     * 使用String的split 方法
     * 	sp为分隔符号，如“,” “|”等
     * @param str
     * @param sp
     * @return
     */
    public static String[] convertStrToArray(String str,String sp){  
        String[] strArray = null;  
        strArray = str.split(sp);  
        return strArray;  
    }

    /**
     * 将long转为指定格式
     * @param number
     * @return
     */
    public static String formatNum(long number) {
		DecimalFormat dFormat = new DecimalFormat("###,###");

		return dFormat.format(number);
	}

    /**
     * 百分比
     * @param num
     * @param all
     * @return
     */
    public static String percent(int num, long all) {
        NumberFormat nt = NumberFormat.getPercentInstance();  
        //设置百分数精确度2即保留两位小数  
        nt.setMinimumFractionDigits(0);  
        float percent = (float)num/all;  
        return nt.format(percent);
    }

    /**
     * 百分比
     * @param num
     * @param all
     * @return
     */
    public static String percent(long num, long all) {
        NumberFormat nt = NumberFormat.getPercentInstance();  
        //设置百分数精确度2即保留两位小数  
        nt.setMinimumFractionDigits(0);  
        float percent = (float)num/all;  
        return nt.format(percent);
    }

    /**
     * 根据url获取腾讯视频id
     * @param url
     * @return
     */
    public static String getTencentVideoId(String url) {
      String pattern = "(\\b\\w+)\\.html";
      Pattern r = Pattern.compile(pattern);
      Matcher m = r.matcher(url);
      if (m.find()) {
        return m.group(1);
      }

      System.out.println("NO MATCH");
      return null;
    }
}
